import $ from 'jquery'
import '@/assets/iconfont/iconfont.css'
export const addCodeBtn = _ => {
  $('pre code').each(function () {
    const lines = $(this).text().split('\n').length - 1
    const $numbering = $('<ol/>').addClass('pre-numbering')
    // let $copy = $('<icon name="bodr"/>').addClass('el-icon-document-copy code-copy')
    const $copy = $('<span class="iconfont icon-copy"></span>').addClass('code-copy')
    $(this)
      .parent()
      .addClass('code')
      .append($numbering)
      .append($copy)
    for (let i = 1; i <= lines; i++) {
      $numbering.append($('<li/>'))
    }
  })
  $('pre.code span.code-copy').click(e => {
    const text = $(e.target).siblings('code').text()
    const element = $('<textarea>' + text + '</textarea>')
    $('body').append(element)
    element[0].select()
    document.execCommand('Copy')
    element.remove()
  })
}
